<template>
  <el-dialog :title="textMap[dialogStatus]"
             :visible.sync="dialogFormVisible2"
             :close-on-click-modal="false"
             :close-on-press-escape="false"
             :fullscreen="false">
    <el-form ref="dataForm"
             :rules="rules"
             :model="formData"
             label-width="130px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="姓名"
                        prop="name">
            <el-input v-model="formData.name" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="性别"
                        prop="gender">
            <el-input v-model="formData.gender" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item label="手机号"
                        prop="mobile">
            <el-input v-model="formData.mobile" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="身份证号"
                        prop="idCardNo">
            <el-input v-model="formData.idCardNo" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="图片路径"
                        prop="imgUrl">
            <el-input v-model="formData.imgUrl" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row class="dialog-btn">
        <el-col :span="24">
          <el-form-item>
            <el-button type="info"
                       @click="dialogFormVisible2 = false"
                       size="mini">取消</el-button>
            <el-button type="primary"
                       size="mini"
                       @click="dialogStatus==='create'?createData():updateData()">保存</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </el-dialog>
</template>

<script>
import * as apiPerson from '@/api/person'

export default {
  name: 'CreatePerson',
  props: ['textMap', 'dialogStatus', 'dialogFormVisible'],
  computed: {
    dialogFormVisible2: {
      get () {
        return this.dialogFormVisible
      },
      set (val) {
        // dialogFormVisible改变由父组件控制
        this.$emit('update:dialogFormVisible', val)
      }
    }
  },
  created () {
  },
  data () {
    return {
      formData: {
        id: undefined,
        tenantId: undefined,
        name: '',
        gender: '',
        mobile: '',
        idCardNo: '',
        imgUrl: ''
      },
      tenantIdFromParent: undefined,
      rules: {
        name: [{ required: true, message: '姓名必填', trigger: 'blur' }],
        gender: [{ required: true, message: '性别必填', trigger: 'blur' }],
        mobile: [{ required: true, message: '手机号必填', trigger: 'blur' }],
        idCardNo: [{ required: true, message: '身份证号必填', trigger: 'blur' }],
        imgUrl: [{ required: true, message: '图片一定要上传', trigger: 'blur' }]
      }
    }
  },
  methods: {
    setTenantId(tenantId) {// 供父组件调用, 传递当前选中的租户
      this.tenantIdFromParent = tenantId
    },
    clearFormData () {//清除 this.formData数据
      Object.keys(this.formData).forEach(key => {
        this.formData[key] = undefined
      })
    },
    setCurrentRow (currentRowData) {// 修改 某条记录时 获取记录信息
      this.formData = Object.assign({}, currentRowData)
    },
    createData () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.formData.tenantId = this.tenantIdFromParent
          apiPerson.createOrUpdate(this.formData).then(() => {
            // 通知父组件刷新列表数据
            this.$emit('on-fresh-list')
            this.dialogFormVisible2 = false
            this.$notify({
              title: '成功',
              message: '创建成功',
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    },
    updateData () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const tempData = Object.assign({}, this.formData)
          apiPerson.createOrUpdate(tempData).then(() => {
            // 通知父组件刷新列表数据
            this.$emit('on-fresh-list')
            this.dialogFormVisible2 = false
            this.$notify({
              title: '成功',
              message: '修改成功',
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    }
  }
}
</script>

<style scoped>
</style>
